package me.moscrop.RootCommand.commands;

import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.Bukkit;

public class Toggle{
	
	public static boolean toggleSender(CommandSender sender, String[] args)
	{
		Player personToToggle;
		
		//if there is arguments
		if(args.length > 0 && (args[0].contentEquals("") == false))
		{
			//get the user and heal them
			personToToggle = Bukkit.getServer().getPlayer(args[0]);
			//if player exists heal them
			if(personToToggle != null)
			{
				
				toggle(personToToggle); // heal and verbose
				((Player) sender).sendMessage(ChatColor.GREEN + personToToggle.getDisplayName() + " is now in game mode " + personToToggle.getGameMode().toString() );
				personToToggle.sendMessage(ChatColor.GREEN + "You are now in game mode " + personToToggle.getGameMode().toString() );

				return true;
			}else{
				((Player) sender).sendMessage(ChatColor.RED + args[0] + " doesnt exist.");
				return true;
			}
		}
		
		//From this point on theres no args so the player wants to feel them self
		
		if((sender instanceof Player) == false)
		{
			sender.sendMessage("This isnt a command for the console silly.");
			return false;
		}
		
		if(args.length == 0 || ( args.length == 1 && args[0].contentEquals("")))
		{
			personToToggle = (Player) sender;
			toggle(personToToggle); //heal and verbose
			personToToggle.sendMessage(ChatColor.GREEN + "You are now in " + personToToggle.getGameMode().toString() + " mode.");
		}
		
		return true;
	}
	
	public static void toggle(Player player)
	{
		if(player.getGameMode() == GameMode.CREATIVE)
		{
			player.setGameMode(GameMode.SURVIVAL);
		}else{
			player.setGameMode(GameMode.CREATIVE);
		}
	}
	
}
